<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表单组件</title>
    <script src="../react/react.development.js"></script>
    <script src="../react/react-dom.development.js"></script>
    <script src="../react/prop-types.js"></script>
    <script src="../react/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
    class MyForm extends React.Component {
        state = {
            subject: [],
            area: '沈阳'
        }

        SingleHandler = (e) => {
            var { name, value } = e.target
            this.setState({
                [name]: value
            })
        }

        MultiTypeHandler = (e) => {
            var { name, value, checked } = e.target
            var arr = name in this.state ? this.state[name] : []
            if (checked) {
                arr.push(value)
            } else {
                arr.splice(arr.findIndex(item => item === value), 1)
            }
            this.setState({
                [name]: arr
            })
        }

        submit = (e) => {
            console.log('提交', this.state)
            e.preventDefault()
        }

        render () {
            return (
                <form>
                    <h1>表单组件</h1>
                    <h2>输入框</h2>
                    <input onInput={this.SingleHandler} name="user" type="text" placeholder="请输入用户名"/><br/>
                    <input onInput={this.SingleHandler} name="pwd" type="password" placeholder="请输入密码"/>
                    <h2>单选框</h2>
                    <input onChange={this.SingleHandler} name="sex" type="radio" value="男"/>男<br/>
                    <input onChange={this.SingleHandler} name="sex" type="radio" value="女"/>女
                    <h2>复选框</h2>
                    <input onChange={this.MultiTypeHandler} type="checkbox" name="subject" value="C语言"/>C语言 <br/>
                    <input onChange={this.MultiTypeHandler} type="checkbox" name="subject" value="Python"/>Python <br/>
                    <input onChange={this.MultiTypeHandler} type="checkbox" name="subject" value="JavaScript"/>JavaScript
                    <h3>{this.state.subject}</h3>
                    <h2>下拉框</h2>
                    <select name="area" onChange={this.SingleHandler}>
                        <option value="沈阳">沈阳</option>
                        <option value="大连">大连</option>
                        <option value="抚顺">抚顺</option>
                        <option value="鞍山">鞍山</option>
                        <option value="本溪">本溪</option>
                    </select><br/>
                    <button type="submit" onClick={this.submit}>submit</button>
                </form>
            )
        }
    }

    ReactDOM.render(<MyForm/>, document.getElementById('root'))
</script>
</body>
</html>
